package com.rran.study.designpattern.behavioral.template.service.spring;

import com.rran.study.designpattern.behavioral.template.repository.ShoppingPersonRepository;
import com.rran.study.designpattern.behavioral.template.service.Shopping;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author yy
 * @Type InitShoppingBean.java
 * @Desc
 * @date 2020/7/21 16:01
 */
@Component("initShoppingBean")
public class InitShoppingBean implements InitializingBean, ApplicationContextAware {

    protected ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() {
        String[] beanNames = applicationContext.getBeanNamesForType(Shopping.class);
        for(String beanName : beanNames){
            ShoppingPersonRepository.storeShopping(beanName, (Shopping) applicationContext.getBean(beanName));
        }
    }
}
